<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-card appearance="outlined" class="settings-card">
  <mat-card-header>
    <mat-card-title>
      <span class="mat-headline-5" translate>admin.general-settings</span>
    </mat-card-title>
  </mat-card-header>
  @if (isLoading$ | async) {
    <mat-progress-bar color="warn" mode="indeterminate">
    </mat-progress-bar>
  }
  @if (!(isLoading$ | async)) {
    <div style="height: 4px;"></div>
  }
  <mat-card-content>
    <form [formGroup]="generalSettingsForm" (ngSubmit)="saveGeneralSettings()" class="tb-form-panel no-border no-padding">
      <fieldset class="fields-group">
        <legend class="group-title" translate>ws-client.ws-client</legend>
        <div class="container">
          <section class="flex flex-row items-center gap-2">
            <mat-slide-toggle formControlName="isLoggingEnabled">
              {{ 'ws-client.show-logs' | translate }}
            </mat-slide-toggle>
            <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
              [matTooltip]="'ws-client.show-logs-hint' | translate"
              matTooltipPosition="right">
              info
            </mat-icon>
          </section>
          <section class="flex flex-row items-center gap-2">
            <mat-form-field class="flex flex-1">
              <mat-label translate>ws-client.messages.max-messages</mat-label>
              <input matInput type="number" min="1" formControlName="maxMessages" placeholder="1000"/>
              <mat-icon matSuffix [matTooltip]="'ws-client.messages.max-messages-hint' | translate">
                info
              </mat-icon>
              @if (generalSettingsForm.get('maxMessages').hasError('required')) {
                <mat-error>
                  {{ 'ws-client.messages.max-messages-required' | translate }}
                </mat-error>
              }
              @if (generalSettingsForm.get('maxMessages').hasError('min')) {
                <mat-error>
                  {{ 'ws-client.messages.max-messages-min' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </section>
        </div>
      </fieldset>
      <div class="layout-wrap flex flex-row items-center justify-end gap-2">
        <button mat-button color="primary"
          [disabled]="generalSettingsForm.pristine"
          (click)="discardGeneralSettings()"
          type="button">{{'action.undo' | translate}}
        </button>
        <button mat-button mat-raised-button color="primary" [disabled]="(isLoading$ | async) || generalSettingsForm.invalid || !generalSettingsForm.dirty"
          type="submit">{{'action.save' | translate}}
        </button>
      </div>
    </form>
  </mat-card-content>
</mat-card>
<mat-card appearance="outlined" class="settings-card">
  <mat-card-header>
    <mat-card-title>
      <span class="mat-headline-5" translate>admin.settings.connectivity-settings</span>
    </mat-card-title>
    <span class="flex flex-1"></span>
    <mat-icon class="material-icons" style="color: rgba(0,0,0,0.18)"
      [matTooltip]="'admin.settings.connectivity-settings-hint' | translate">info
    </mat-icon>
  </mat-card-header>
  <mat-card-content>
    <section class="tb-form-panel no-border no-padding">
      <div class="tb-form-panel no-border no-padding toggle-group">
        <tb-toggle-select appearance="fill" [(ngModel)]="protocol">
          <tb-toggle-option value="mqtt">{{ 'admin.settings.mqtt-mqtts' | translate }}</tb-toggle-option>
          <tb-toggle-option value="ws">{{ "admin.settings.ws-wss" | translate }}</tb-toggle-option>
        </tb-toggle-select>
        <div class="tb-form-hint tb-primary-fill">{{ 'admin.settings.connectivity-hint' | translate }}</div>
      </div>
      <form [formGroup]="connectivitySettingsForm" (ngSubmit)="saveConnectivitySettings()" class="tb-form-panel no-border no-padding">
        @if (protocol === 'ws') {
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol}"></ng-container>
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol + 's'}"></ng-container>
        }
        @if (protocol === 'mqtt') {
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol}"></ng-container>
          <ng-container *ngTemplateOutlet="connectivitySettings; context:{protocol: protocol + 's'}"></ng-container>
        }
        <ng-template #connectivitySettings let-protocol="protocol">
          <div class="tb-form-panel stroked no-padding-bottom" [formGroupName]="protocol">
            <mat-slide-toggle formControlName="enabled">
              {{ 'admin.settings.' + protocol | translate }}
            </mat-slide-toggle>
            <div class="tb-form-row column-xs no-border no-padding tb-standard-fields">
              <mat-form-field class="flex flex-1" hideRequiredMarker>
                <mat-label translate>admin.settings.host</mat-label>
                <input matInput formControlName="host"/>
                @if (connectivitySettingsForm.get(protocol + '.host').hasError('required')) {
                  <mat-error>
                    {{ 'admin.settings.host-required' | translate }}
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="flex flex-1" hideRequiredMarker>
                <mat-label translate>admin.settings.port</mat-label>
                <input matInput type="number" min="0" max="65535" formControlName="port"/>
                @if (connectivitySettingsForm.get(protocol + '.port').hasError('required')) {
                  <mat-error>
                    {{ 'admin.settings.port-required' | translate }}
                  </mat-error>
                }
                @if (connectivitySettingsForm.get(protocol + '.port').hasError('pattern')) {
                  <mat-error>
                    {{ 'admin.settings.port-pattern' | translate }}
                  </mat-error>
                }
                @if (connectivitySettingsForm.get(protocol + '.port').hasError('min') ||
                  connectivitySettingsForm.get(protocol + '.port').hasError('max')) {
                  <mat-error>
                    {{ 'admin.settings.port-range' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
          </div>
        </ng-template>
        <div class="layout-wrap flex flex-row items-center justify-end gap-2">
          <button mat-button color="primary"
            [disabled]="connectivitySettingsForm.pristine"
            (click)="discardConnectivitySettings()"
            type="button">{{'action.undo' | translate}}
          </button>
          <button mat-button mat-raised-button color="primary"
            [disabled]="(isLoading$ | async) || connectivitySettingsForm.invalid || !connectivitySettingsForm.dirty"
            type="submit">{{'action.save' | translate}}
          </button>
        </div>
      </form>
    </section>
  </mat-card-content>
</mat-card>
